package com.myupload.demo.dao;

import ch.qos.logback.core.util.FileUtil;
import com.myupload.demo.service.UploadService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Controller
public class UpLoadController {
    @Autowired
    UploadService uploadService;


    @PostMapping("/upload/file")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            return "文件有误";
        }
        //1：获取用户指定的文件夹，问这个文件夹为什么要从页面传过来呢？
        //原因是：做隔离，不同业务，不同文件放在不同的目录中
        long size=multipartFile.getSize();
        System.out.println("size = " + size);
        String name = multipartFile.getName();
        System.out.println("name = " + name);
        String dir=request.getParameter("dir");
        return uploadService.UploadImg(multipartFile,dir);
    }
    @PostMapping("/upload/file2")
    @ResponseBody
    public Map<String,Object> uploadGmap(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            return null;
        }
        //1：获取用户指定的文件夹，问这个文件夹为什么要从页面传过来呢？
        //原因是：做隔离，不同业务，不同文件放在不同的目录中
        long size=multipartFile.getSize();
        System.out.println("size = " + size);
        String name = multipartFile.getName();
        System.out.println("name = " + name);
        String dir=request.getParameter("dir");
        return uploadService.UploadImgGMap(multipartFile, dir);
    }
}
